package com.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class QueryUtils<T> {
    public IPage<T> getPage(Query queryParams) {
        return this.getPage(queryParams, null, false);
    }

    public IPage<T> getPage(Query queryParams, String defaultOrderField, boolean isAsc) {
        //分页参数
        long curPage = 1;
        long pageSize = 10;

        if (queryParams.getCurrentPage() != null) {
            curPage = queryParams.getCurrentPage();
        }
        if (queryParams.getPageSize() != null) {
            pageSize = queryParams.getPageSize();
        }

        //分页对象
        Page<T> page = new Page<>(curPage, pageSize);

        //排序字段驼峰转下划线
        //String orderField = StringUtil.humpToUnderline((String) params.get(Constant.ORDER_FIELD));
        String sortByField = queryParams.getSortByField();
        String order = queryParams.getOrder();
        //前端字段排序
        if (StringUtils.isNotEmpty(sortByField) && StringUtils.isNotEmpty(order)) {
            if ("asc".equalsIgnoreCase(order)) {
                return page.addOrder(OrderItem.asc(sortByField.trim()));
            } else {
                return page.addOrder(OrderItem.desc(sortByField.trim()));
            }
        }

        //没有排序字段，则不排序
        if (StringUtils.isBlank(defaultOrderField)) {
            return page;
        }
        //默认排序
        if (isAsc) {
            page.addOrder(OrderItem.asc(defaultOrderField));
        } else {
            page.addOrder(OrderItem.desc(defaultOrderField));
        }
        return page;
    }

    public QueryWrapper<T> getWrapper(Object queryParams, Class<T> bean) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
        try {
            Map<String, String> tempParams = BeanUtils.describe(queryParams);
            if (tempParams == null) {
                return queryWrapper;
            }
            Map<String, Object> map = describe(bean, tempParams);
            Set<String> keySet = map.keySet();
            for (String key : keySet) {
                Object o = map.get(key);
                String value = null;
                if (o != null) {
                    value = o.toString().trim();
                }
                if (StringUtils.isNotEmpty(value)) {
                    queryWrapper = queryWrapper.eq(key, value);
                }
            }
        } catch (IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return queryWrapper;
    }

    //封装查询参数并把驼峰命名转为下划线名字 去掉前后空格
    private <E> Map<String, Object> describe(Class<T> beanClass, Map<String, String> params) throws IllegalAccessException, ClassNotFoundException {
        Map<String, Object> map = new HashMap<>();
        Field[] fields = beanClass.getDeclaredFields();
        for (Field field : fields) {
            Object o = params.get(field.getName());
            if (o != null && o.toString().trim().length() > 0) {
                String value = o.toString().trim();
                map.put(field.getName(), value);
            }
        }
        return map;
    }
}
